package com.cy.aircompressor.main.ui.onlinemonitor.provider;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.cy.aircompressor.databinding.ItemVibrationChangeTrendBinding;
import com.cy.aircompressor.entity.sensor.VibrationDataChangeTrend;
import com.cy.aircompressor.main.ui.onlinemonitor.utils.LineChartUtil;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.github.mikephil.charting.data.Entry;
import com.net.model.base.BaseBindViewHolder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class VibrationChangeTrendProvider extends ItemViewBinder<VibrationDataChangeTrend, VibrationChangeTrendProvider.ViewHolder> {
    Context mContext;

    public VibrationChangeTrendProvider(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    protected VibrationChangeTrendProvider.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemVibrationChangeTrendBinding.inflate(inflater, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull VibrationChangeTrendProvider.ViewHolder holder, @NonNull VibrationDataChangeTrend entity) {
        holder.mBinding.partsName.setText(entity.getPartName());

        List<Entry> entries = getChartEntry(entity.getXspeed());
        holder.mBinding.vibrationX.setText("x轴speed曲线");
//        LineChartUtil.addLine(holder.mBinding.vibrationChartX, entries,"#3C6579");
        entries = getChartEntry(entity.getXrms());
        holder.mBinding.vibrationXrms.setText("x轴rms曲线");
//        LineChartUtil.addLine(holder.mBinding.vibrationChartXrms, entries,"#3C6579");


        entries = getChartEntry(entity.getYspeed());
        holder.mBinding.vibrationY.setText("y轴speed曲线");
//        LineChartUtil.addLine(holder.mBinding.vibrationChartY, entries,"#3C6579");
        entries = getChartEntry(entity.getYrms());
        holder.mBinding.vibrationYrms.setText("y轴rms曲线");
//        LineChartUtil.addLine(holder.mBinding.vibrationChartYrms, entries,"#3C6579");


        entries = getChartEntry(entity.getZspeed());
        holder.mBinding.vibrationZ.setText("z轴speed曲线");
//        LineChartUtil.addLine(holder.mBinding.vibrationChartZ, entries,"#3C6579");
        entries = getChartEntry(entity.getZrms());
        holder.mBinding.vibrationZrms.setText("z轴rms曲线");
//        LineChartUtil.addLine(holder.mBinding.vibrationChartZrms, entries,"#3C6579");
    }

    private List<Entry> getChartEntry(List<VibrationDataChangeTrend.VibrationData> vibrationDatas) {
        List<Entry> entries = new ArrayList<>();
        if (vibrationDatas != null && vibrationDatas.size() != 0) {
            for (int i = 0; i < vibrationDatas.size(); i++) {
                VibrationDataChangeTrend.VibrationData vibrationData = vibrationDatas.get(i);
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = format.parse(vibrationData.getDate());
                    Entry entry = new Entry(i, vibrationData.getValue(), date.getTime());
                    entries.add(entry);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        return entries;
    }

    public class ViewHolder extends BaseBindViewHolder<ItemVibrationChangeTrendBinding> {

        protected ViewHolder(ItemVibrationChangeTrendBinding itemView) {
            super(itemView);

            LineChartUtil.initLineChart(mBinding.vibrationChartX, false);
            LineChartUtil.initLineChart(mBinding.vibrationChartXrms, false);
            LineChartUtil.initLineChart(mBinding.vibrationChartY, false);
            LineChartUtil.initLineChart(mBinding.vibrationChartYrms, false);
            LineChartUtil.initLineChart(mBinding.vibrationChartZ, false);
            LineChartUtil.initLineChart(mBinding.vibrationChartZrms, false);
        }
    }


}
